﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;

namespace OWA
{
    /// <summary>
    /// Manage confirguration values stored in registry
    /// </summary>
    public static class Configuration
    {
        /// <summary>
        /// Gets the registry key
        /// </summary>
        private static RegistryKey Key
        {
            get
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\dvoituron\\OWA", true);
                if (key == null)
                    key = Registry.CurrentUser.CreateSubKey("Software\\dvoituron\\OWA");
                return key;
            }
        }

        /// <summary>
        /// Sets or Gets the WebMail URL
        /// </summary>
        public static string Url
        {
            get
            {
                return Key.GetValue("Url", "https://webmail.youcompany.com/").ToString();
            }

            set
            {
                Key.SetValue("Url", value);
            }
        }

        /// <summary>
        /// Sets or Gets the WebMail version
        /// </summary>
        public static string Version
        {
            get
            {
                return Key.GetValue("Version", "2003").ToString();
            }

            set
            {
                Key.SetValue("Version", value);
            }
        }

        /// <summary>
        /// Sets or Gets the WebMail Login
        /// </summary>
        public static string Login
        {
            get
            {
                return Key.GetValue("Login", string.Empty).ToString();
            }

            set
            {
                Key.SetValue("Login", value);
            }
        }

        /// <summary>
        /// Sets or Gets the WebMail Password
        /// </summary>
        public static string Password
        {
            get
            {
                string password = Key.GetValue("Password", string.Empty).ToString();
                return String.IsNullOrEmpty(password) ? string.Empty : Base64.Decode(password);
            }

            set
            {
                Key.SetValue("Password", Base64.Encode(value));
            }
        }

        /// <summary>
        /// Gets or Sets the form mode (modal or modeless)
        /// </summary>
        public static bool ModalForm
        {
            get
            {
                if (Key.GetValue("ModalForm", "1").ToString() == "1")
                    return true;
                else
                    return false;
            }

            set
            {
                if (value)
                    Key.SetValue("ModalForm", "1");
                else
                    Key.SetValue("ModalForm", "0");
            }
        }
    }
}
